package com.kakame.netbook.front.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.kakame.netbook.common.models.WordErrorRecord;
import com.kakame.netbook.common.models.WordInfo;
import com.kakame.netbook.common.utils.CommonUtil;

public class IndexController extends Controller {
	
	public void index() {
		
		setAttr("wordMap", getWordMap(null));
		
		renderTemplate("index.html");
	}
	
	private Map<String, List<WordInfo>> getWordMap(String searchWord) {
		List<WordInfo> list = null;
		if(searchWord == null || "".equals(searchWord.trim())) {
			list = WordInfo.dao.find("SELECT * FROM t_word_info ORDER BY word_begin ASC");
		} else {
			list = WordInfo.dao.find("SELECT * FROM t_word_info WHERE word like ? ORDER BY word_begin ASC", "%" + searchWord + "%");
		}
		
		Map<String, List<WordInfo>> wordMap = new TreeMap<>();
		
		for (WordInfo word : list) {
			
			String wordBegin = word.getWordBegin();
			
			List<WordInfo> item = wordMap.get(wordBegin);
			
			if(item == null) {
				item = new ArrayList<>();
			}
			
			item.add(word);
			
			wordMap.put(wordBegin, item);
		}
		return wordMap;
	}
	
	public void list() {
		renderJson(getWordMap(getPara("word")));
	}
	
	public void addView() {
		setAttr("type", "add");
		renderTemplate("add.html");
	}

	public void checkView() {
		renderTemplate("check.html");
	}

	public void add() {
		WordInfo wordInfo = getBean(WordInfo.class, "");
		wordInfo.setMemoryNum(0);
		wordInfo.setCreateTime(new Date());
		wordInfo.setStatus("C");
		wordInfo.save();
		renderJson();
	}
	
	public void updateWord() {
		WordInfo wordInfo = getBean(WordInfo.class, "");
		wordInfo.setMemoryNum(wordInfo.getMemoryNum() + 1);
		wordInfo.setModifyTime(new Date());
		wordInfo.update();
		renderJson();
	}
	
	public void wordDetail() {
		String wordId = getPara(0);
		WordInfo wordInfo = WordInfo.dao.findById(wordId);
		setAttr("word", wordInfo);
		setAttr("type", "detail");
		List<WordErrorRecord> errorList = WordErrorRecord.dao.find("select * from t_word_error_record where word = ?", wordInfo.getWord());
		int errorE2C = 0;
		int errorC2E = 0;
		for (WordErrorRecord wordErrorRecord : errorList) {
			if("E2C".equals(wordErrorRecord.getErrorType())) {
				errorE2C = wordErrorRecord.getErrorCount();
			}
			if("C2E".equals(wordErrorRecord.getErrorType())) {
				errorC2E = wordErrorRecord.getErrorCount();
			}
		}
		setAttr("errorE2C", errorE2C);
		setAttr("errorC2E", errorC2E);
		renderTemplate("add.html");
	}
	
	public void jump2check() {
		String pageType = getPara(0);
		setAttr("pageType", pageType);
		renderTemplate("jump2check.html");
	}
	public void checkOnE2C() {
		
		Integer wordNum = getParaToInt(0);
		String deep = getPara(1);
		
		List<WordInfo> allList = WordInfo.dao.find("SELECT * FROM t_word_info where instr(?, memory_deep) > 0", deep);
		
		Collections.shuffle(allList);
		
		int answerNum = 4;
		
		HashSet<Integer> set = null;
		
		List<WordInfo> qasList = allList;
		if(allList.size() > wordNum) {
			qasList = allList.subList(0, wordNum);
		}
		
		
		for (int i = 0; i < qasList.size(); i++) {
			
			set = new HashSet<Integer>();
			CommonUtil.randomSet(0, allList.size(), answerNum - 1, i, set);
			
			List<Map<String, String>> answers = new ArrayList<>();
			
			for (Integer index : set) {
				Map<String, String> map = new HashMap<>();
				map.put("value", allList.get(index).getWord());
				map.put("text", allList.get(index).getComment());
				answers.add(map);
			}
			Map<String, String> map = new HashMap<>();
			map.put("value", qasList.get(i).getWord());
			map.put("text", qasList.get(i).getComment());
			map.put("correct", "yes");
			answers.add(map);
			
			Collections.shuffle(answers);
			
			qasList.get(i).setAnswers(answers);
			
			if(i == 0) {
				qasList.get(i).setShow("true");
			} else {
				qasList.get(i).setShow("false");
			}
		}
		
		setAttr("qasList", qasList);
		
		renderTemplate("checkOnE2C.html");
	}
	public void checkOnC2E() {
		
		Integer wordNum = getParaToInt(0);
		
		String deep = getPara(1);
		
		List<WordInfo> allList = WordInfo.dao.find("SELECT * FROM t_word_info where instr(?, memory_deep) > 0", deep);
		
		Collections.shuffle(allList);
		
		int answerNum = 4;
		
		HashSet<Integer> set = null;
		
		List<WordInfo> qasList = allList;
		if(allList.size() > wordNum) {
			qasList = allList.subList(0, wordNum);
		}
		
		for (int i = 0; i < qasList.size(); i++) {
			
			set = new HashSet<Integer>();
			CommonUtil.randomSet(0, allList.size(), answerNum - 1, i, set);
			
			List<Map<String, String>> answers = new ArrayList<>();
			
			for (Integer index : set) {
				Map<String, String> map = new HashMap<>();
				map.put("value", allList.get(index).getWord());
				map.put("text", allList.get(index).getWord());
				answers.add(map);
			}
			Map<String, String> map = new HashMap<>();
			map.put("value", qasList.get(i).getWord());
			map.put("text", qasList.get(i).getWord());
			map.put("correct", "yes");
			answers.add(map);
			
			Collections.shuffle(answers);
			
			qasList.get(i).setAnswers(answers);
			
			if(i == 0) {
				qasList.get(i).setShow("true");
			} else {
				qasList.get(i).setShow("false");
			}
		}
		
		setAttr("qasList", qasList);
		
		renderTemplate("checkOnC2E.html");
	}
	@SuppressWarnings("unchecked")
	public void insertErrorWord() {
		String para = getPara("errorWords");
		List<Map<String, Object>> list = FastJson.getJson().parse(para, List.class);
		for (Map<String, Object> map : list) {
			WordErrorRecord find = WordErrorRecord.dao.findFirst("select * from t_word_error_record where word = ? and error_type = ?", map.get("word"), map.get("errorType"));
			if(find != null) {
				WordErrorRecord w = new WordErrorRecord();
				w.setId(find.getId());
				w.setErrorCount(find.getErrorCount() + 1);
				w.setModifyTime(new Date());
				w.update();
			} else {
				WordErrorRecord w = new WordErrorRecord();
				w.setWord(String.valueOf(map.get("word")));
				w.setErrorType(String.valueOf(map.get("errorType")));
				w.setErrorCount(Integer.parseInt(String.valueOf(map.get("errorCount"))));
				w.setCreateTime(new Date());
				w.setStatus("C");
				w.save();
			}
		}
		renderJson();
	}
}